package tripx.db.system;

import java.io.File;
import java.io.IOException;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.apache.xmlrpc.XmlRpcException;
import org.apache.xmlrpc.server.PropertyHandlerMapping;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class SysConfig {

	private Document configDoc;

	private String userName;

	private String host;

	private String password;

	private String dbName;

	private String protocol;

	private String driverClass;

	private String dbPort;

	private int serverPort;

	private PropertyHandlerMapping pMapping;

	public SysConfig() {
		pMapping = new PropertyHandlerMapping();
		try {
			configDoc = DocumentBuilderFactory.newInstance()
					.newDocumentBuilder().parse(new File("./config/setup.xml"));
		} catch (SAXException | IOException | ParserConfigurationException e) {

		}
		initAll();
	}

	private void initAll() {
		Element root = configDoc.getDocumentElement();

		Logger logger = Logger.getInstance();
		logger.info("Read Configuration Begins");

		dbName = root.getElementsByTagName("db").item(0).getAttributes()
				.item(0).getNodeValue();
		logger.info("\tDatabase Name: " + dbName);
		userName = ((Element) root.getElementsByTagName("user").item(0))
				.getAttribute("name");
		logger.info("\tUser Name: " + userName);
		password = ((Element) root.getElementsByTagName("user").item(0))
				.getAttribute("password");
		logger.info("\tPassword: " + password);
		driverClass = ((Element) root.getElementsByTagName("driver").item(0))
				.getAttribute("class");
		logger.info("\tDriver Class: " + driverClass);
		protocol = ((Element) root.getElementsByTagName("url").item(0))
				.getAttribute("protocol");
		logger.info("\tProtocol: " + protocol);
		host = ((Element) root.getElementsByTagName("url").item(0))
				.getAttribute("host");
		logger.info("\tHost: " + host);
		dbPort = ((Element) root.getElementsByTagName("url").item(0))
				.getAttribute("port");
		logger.info("\tDatabase Port: " + dbPort);
		serverPort = Integer.parseInt(root.getElementsByTagName("server")
				.item(0).getAttributes().item(0).getNodeValue());
		logger.info("\tServer Port: " + serverPort);
		NodeList pmList = root.getElementsByTagName("handler");
		for (int i = 0; i < pmList.getLength(); i++) {
			Element e = (Element) pmList.item(i);
			try {
				pMapping.addHandler(e.getAttribute("key"),
						Class.forName(e.getAttribute("class")));
			} catch (ClassNotFoundException | XmlRpcException e1) {

			}
		}
		logger.info("Read Configuration Ends");
	}

	public String getUserName() {
		return userName;
	}

	public String getHost() {
		return host;
	}

	public String getPassword() {
		return password;
	}

	public String getDbName() {
		return dbName;
	}

	public String getProtocol() {
		return protocol;
	}

	public String getDriverClass() {
		return driverClass;
	}

	public String getDbPort() {
		return dbPort;
	}

	public int getServerPort() {
		return serverPort;
	}

	public PropertyHandlerMapping getPropertyHandlerMapping() {
		return pMapping;
	}

}
